package com.etogc.thirdsdk.rong;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;

import com.huawei.hms.api.ConnectionResult;
import com.huawei.hms.api.HuaweiApiClient;
import com.huawei.hms.support.api.push.HuaweiPush;
import com.xiaomi.mipush.sdk.MiPushClient;

import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;

/**
 * Created by ZeroVoid on 2018-2-27.
 */

public class RongUtil {
    /**
     * 开关华为推送
     *
     * @param isOpen
     */
    public static void enableHuaWeiPush(Context context, boolean isOpen) {
        HuaweiApiClient client = new HuaweiApiClient.Builder(context)
                .addApi(HuaweiPush.PUSH_API)
                .addConnectionCallbacks(new HuaweiApiClient.ConnectionCallbacks() {
                    @Override
                    public void onConnected() {

                    }

                    @Override
                    public void onConnectionSuspended(int i) {

                    }
                })
                .addOnConnectionFailedListener(new HuaweiApiClient.OnConnectionFailedListener() {
                    @Override
                    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

                    }
                })
                .build();
        client.connect();

        if (!client.isConnected()) {
            return;
        }
        HuaweiPush.HuaweiPushApi.enableReceiveNormalMsg(client, isOpen);
    }

    public static void switchNotice(Context context, boolean isEnable) {
        if (isEnable) {
            MiPushClient.enablePush(context);
            enableHuaWeiPush(context, true);
        } else {
            MiPushClient.disablePush(context);
            enableHuaWeiPush(context, false);
        }
//        RongIM.
    }

    //获取融云的token之后,连接到融云的IM服务器
    public static void connect_rong(final Activity act, String token) {
        RongIM.connect(token, new RongIMClient.ConnectCallback() {
            @Override
            public void onTokenIncorrect() {
                Log.e("LoginActivity", "onTokenIncorrect");
            }

            @Override
            public void onSuccess(String userid) {
                Log.e("LoginActivity", "连接融云IM成功,userid=" + userid);
//                jumpMain();
                act.finish();
//                sendMsg();
            }

            @Override
            public void onError(RongIMClient.ErrorCode errorCode) {
                Log.e("LoginActivity", "ErrorCode");
            }


        });

    }

}
